package com.sky.controller.admin;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author 衷岚
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品信息：{}", dishDTO);
        dishService.addWithFlavor(dishDTO);

        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result pageQuery(DishPageQueryDTO dishPageQueryDTO){

        log.info("菜品分类查询数据：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("菜品状态：{}", status);
        dishService.updateStatus(status,id);
        return Result.success();

    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result list(Long categoryId) {
        log.info("菜品类别id:{}", categoryId);
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        return Result.success(dishList);
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        log.info("根据id查询菜品菜品id:{}",id);
        DishVO dishVO = dishService.findWithFlavorById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品ids:{}", ids);
        dishService.delete(ids);
        return Result.success();
    }

    @ApiOperation("修改菜品")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品相关参数：{}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();

    }


}
